package terra.ui.wnd;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.GUIContext;
/** Абстрактный класс, описывающий составное окно, имеющее внутри себя
 * интерактивные компоненты
 */
public abstract class CompoundWindow extends AbstractWindow implements ComponentListener{

	ArrayList<AbstractComponent> elements;
	public AbstractComponent focusedElement;
	protected long lastFocusedItem = 0;
	
	public CompoundWindow(GUIContext container, ComponentListener listener)
	{
		super(container, listener);
		elements = new ArrayList<AbstractComponent>();
	}

	@Override
	public void setLocation(int x, int y) {
		if (getArea() == null)
			return;
		int oldx = getX();
		int oldy = getY();
		super.setLocation(x, y);
		for(AbstractComponent element : elements)
			element.setLocation(element.getX() + getX()-oldx, element.getY() + getY()-oldy);

	}
	
	
	protected void addElement(AbstractComponent element)
	{
		elements.add(element);
	}

	public void render(GUIContext container, Graphics g) throws SlickException {
		for(AbstractComponent el :elements)
			el.render(container, g);
		super.render(container, g);
		g.setColor(Color.red);
		//if (focusedElement != null)
		//g.drawRect(focusedElement.getX(),focusedElement.getY(),focusedElement.getWidth(),focusedElement.getHeight());
	}
	
	@Override
	public void mouseDragged(int oldx, int oldy, int newx, int newy) {
		if (hasFocus())
		if (focusedElement != null && isVisible())
		{
			focusedElement.mouseDragged(oldx, oldy, newx, newy);
			return;
		}
		super.mouseDragged(oldx, oldy, newx, newy);
	}
	
	@Override
	public void mouseMoved(int oldx, int oldy, int newx, int newy) {
		super.mouseMoved(oldx, oldy, newx, newy);
		if (hasFocus())
			focusedElement = hasFocus(newx,newy);
		if (focusedElement != null && isVisible())
			focusedElement.mouseMoved(oldx, oldy, newx, newy);
	}
	
	@Override
	public void mousePressed(int button, int mx, int my){
		super.mousePressed(button, mx, my);
		if (focusedElement != null && isVisible()){
			focusedElement.mousePressed(button, mx, my);
			consumeEvent();
		}
	}
	
	@Override
	public void mouseReleased(int button, int mx, int my)
	{
		super.mouseReleased(button, mx, my);
		if (focusedElement != null && isVisible()){
			focusedElement.mouseReleased(button, mx, my);
			consumeEvent();
		}
	}
	
	protected AbstractComponent hasFocus(int x, int y)
	{
		for(AbstractComponent el : elements)
			if (Rectangle.contains(x, y, el.getX(), el.getY(), el.getWidth(), el.getHeight()))
			{
				if (focusedElement != el)
					lastFocusedItem = System.currentTimeMillis();
				return el;
			}
		return null;
		
	}
	
	public void focusLost()
	{
		focusedElement = null;
		lastFocusedItem = 0;
	}
	
	
}
